<?php defined('SYSPATH') or die('No direct script access.');

/**
 * @file userpass.php
 * @brief 用户登录登出
 * @author Lionel Tsai
 * @version 1.0
 * @date 2012-02-17
 */
class Controller_Userpass extends Controller_Rest {
	/**
	 * @brief 用户登录
	 *
	 * @return $this
	 */
	public function action_login_post()
	{
		$user = array('username', 'password', 'rememberme');
		foreach($user as $field) {
			$username = $this->request->post('username');
			if($username === NULL) {
				$this->response->body("{$field} not matched!")->status(203);
				return;
			}
			$postuser = $this->request->post();
			$auth = Auth::instance();
			if(!$auth->ed_login($postuser))
			{
				$this->response->body("{$postuser['username']} login failed!")->status(203);
				return;
			}
			$ret = json_encode(array( 'sid' => $auth->get_sid(), 'username' =>$postuser['username'] ));
			$this->response->body($ret)->status(200);
			//$this->response('200', array( 'sid' => $auth->get_sid() ));
		}
	}
	/**
	 * @brief 用户登出
	 *
	 * @return 
	 */
	public function action_logout_get()
	{
		if( !isset($_GET['sid']) )
		{
			$this->response->body('no login')->status(201);
			return;
		}

		$auth = Auth::instance($_GET['sid']);
		if( !$auth->logged_in() )
		{
			$this->response->body('already logout')->status(201);
			return;
		}

		$auth->logout(TRUE);
		$this->response->body('logout success')->status(200);
	}
} // End Userpass
